package com.future.offer;

import com.future.util.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 剑指 Offer II 050. 向下的路径节点之和
 *
 * @author weiruibai.vendor
 * Date: 2023/1/11 09:31
 */
public class Solution_050 {

    public int pathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return 0;
        }
        List<Long> list = new ArrayList<>();
        getPathSum(root, list, targetSum);
        return ans;
    }

    private void getPathSum(TreeNode root, List<Long> list, int targetSum) {
        if (root == null) {
            return;
        }
        Long val = Long.valueOf(root.val);
        List<Long> newList = new ArrayList<>();
        for (Long num : list) {
            Long sum = num + val;
            if (sum <= Integer.MAX_VALUE && sum >= Integer.MIN_VALUE) {
                if (targetSum == sum.intValue()) {
                    ans++;
                }
            }
            newList.add(sum);
        }
        if (val.intValue()==targetSum){
            ans++;
        }
        newList.add(val);
        getPathSum(root.left, newList, targetSum);
        getPathSum(root.right, newList, targetSum);
    }
    int ans = 0;
}
